/*
 * Copyright (C) 1994-2021 Altair Engineering, Inc.
 * For more information, contact Altair at www.altair.com.
 *
 * This file is part of both the OpenPBS software ("OpenPBS")
 * and the PBS Professional ("PBS Pro") software.
 *
 * Open Source License Information:
 *
 * OpenPBS is free software. You can redistribute it and/or modify it under
 * the terms of the GNU Affero General Public License as published by the
 * Free Software Foundation, either version 3 of the License, or (at your
 * option) any later version.
 *
 * OpenPBS is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public
 * License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * Commercial License Information:
 *
 * PBS Pro is commercially licensed software that shares a common core with
 * the OpenPBS software.  For a copy of the commercial license terms and
 * conditions, go to: (http://www.pbspro.com/agreement.html) or contact the
 * Altair Legal Department.
 *
 * Altair's dual-license business model allows companies, individuals, and
 * organizations to create proprietary derivative works of OpenPBS and
 * distribute them - whether embedded or bundled with other software -
 * under a commercial license agreement.
 *
 * Use of Altair's trademarks, including but not limited to "PBS™",
 * "OpenPBS®", "PBS Professional®", and "PBS Pro™" and Altair's logos is
 * subject to Altair's trademark licensing policies.
 */

#include <pbs_config.h> /* the master config generated by configure */
#include <pbs_version.h>
#include <assert.h>
#include <pwd.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <libgen.h>
#include <dirent.h>
#include <errno.h>
#include "libpbs.h"
#include "portability.h"
#include "ticket.h"
#include "server_limits.h"
#include "pbs_db.h"

/**
 * @brief
 *	The main function in C - entry point
 *
 * @param[in]  argc - argument count
 * @param[in]  argv - pointer to argument array
 *
 * @return  int
 * @retval  0 - success
 * @retval  !0 - error
 */
int
main(int argc, char *argv[])
{
	int rc;
	int i;
	int errflg = 0;
	char prog[] = "pbs_dataservice";
	char sopt[256];
	char conn_db_host[PBS_MAXSERVERNAME + 1];
	char *errmsg = NULL;

	/* test for real deal or just version and exit */
	PRINT_VERSION_AND_EXIT(argc, argv);
	while ((i = getopt(argc, argv, "s:")) != EOF) {
		switch (i) {
			case 's':
				pbs_strncpy(sopt, optarg, sizeof(sopt));
				break;
			case '?':
			default:
				errflg++;
		}
	}
	if (errflg) {
		fprintf(stderr, "\nusage: %s -s [start|stop|status]\n", prog);
		return (-1);
	}
	/* read configuration file */
	if (pbs_loadconf(0) == 0) {
		fprintf(stderr, "%s: Could not load pbs configuration\n", prog);
		return (-1);
	}

	/* check admin privileges */
	if (getuid() != 0 || geteuid() != 0) {
		fprintf(stderr, "%s: Must be run by root\n", prog);
		return (1);
	}

	if (pbs_conf.pbs_data_service_host)
		pbs_strncpy(conn_db_host, pbs_conf.pbs_data_service_host, PBS_MAXSERVERNAME);
	else
		pbs_strncpy(conn_db_host, pbs_default(), PBS_MAXSERVERNAME);

	if (strcmp(sopt, PBS_DB_CONTROL_START) == 0) {
		rc = pbs_start_db(conn_db_host, pbs_conf.pbs_data_service_port);
		if (rc == PBS_DB_OOM_ERR)
			rc = 0; /* Ignore OOM access error */
	} else if (strcmp(sopt, PBS_DB_CONTROL_STOP) == 0) {
		rc = pbs_stop_db(conn_db_host, pbs_conf.pbs_data_service_port);
	} else if (strcmp(sopt, PBS_DB_CONTROL_STATUS) == 0) {
		rc = pbs_status_db(conn_db_host, pbs_conf.pbs_data_service_port);
		if (rc) {
			pbs_db_get_errmsg(PBS_DB_ERR, &errmsg);
			if (errmsg) {
				fprintf(stderr, "%s: %s", prog, errmsg);
				free(errmsg);
			}
		}
	} else {
		fprintf(stderr, "\nusage: %s -s [start|stop|status]\n", prog);
		return -1;
	}
	return (rc);
}
